/**
 * Project Name:fire-industry-DM-center
 * File Name:RoleServer.java
 * Package Name:com.firestone.web.center
 * Date:2018年3月29日下午6:44:15
 *
*/

package com.firestone.web.center;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.firestone.bean.dto.PageDTO;
import com.firestone.bean.dto.ReturnDTO;
import com.firestone.bean.dto.role.RoleDTO;
import com.firestone.bean.dto.user.User;
import com.firestone.bean.entity.mysql.primary.ImCenterRole;
import com.firestone.bean.po.role.SavePO;
import com.firestone.bean.po.role.UpdatePO;
import com.firestone.bean.vo.BaseListVO;
import com.firestone.bean.vo.role.RoleVO;
import com.firestone.converters.center.ConverterRole;
import com.firestone.core.enums.SystemCodeEnum;
import com.firestone.service.center.IImCenterRoleService;
import com.firestone.web.BaseServer;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: TODO(角色控制器)
 * @author CAIYJ
 * @date 2018年3月29日 下午6:44:15
 */
@Api(value = "角色控制器", tags = { "角色接口文档" })
@RestController
@RequestMapping(value = "/role")
public class RoleServer extends BaseServer {

    @Autowired
    private IImCenterRoleService imCenterRoleService;

    /**
     * @Description: TODO(角色列表)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:53:23
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "列表", notes = "列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "角色名称", required = false, dataType = "String", paramType = "query") })
    @RequestMapping(value = "/list")
    public ReturnDTO<BaseListVO<RoleVO>> list(String name, PageDTO pageDTO)
            throws Exception {
        Page<RoleDTO> page = new Page<>(pageDTO.getPage(), pageDTO.getSize());
        page = imCenterRoleService.listRole(page, name);
        return ReturnDTO.success(ConverterRole.convertListVO(page));
    }

    /**
     * @Description: TODO(新增角色)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:53:34
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/save")
    @Transactional
    public ReturnDTO<String> save(SavePO save) throws Exception {
        ImCenterRole role = new ImCenterRole();
        BeanUtils.copyProperties(save, role);
        User user = getUserInfo();
        role.setCreator(user.getId());
        if (imCenterRoleService.saveRole(role, save.getMenuId())) {
            return ReturnDTO.success("新增成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_create_failed);
    }

    /**
     * @Description: TODO(更新角色信息)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:53:44
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = false, dataType = "Long", paramType = "path") })
    @RequestMapping(value = "/update/{id}")
    public ReturnDTO<String> update(@PathVariable Long id, UpdatePO update)
            throws Exception {
        ImCenterRole role = new ImCenterRole();
        BeanUtils.copyProperties(update, role);
        role.setId(id);
        User user = getUserInfo();
        role.setReviser(user.getId());
        if (imCenterRoleService.updateRole(role, update.getMenuId())) {
            return ReturnDTO.success("修改成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_create_failed);
    }

    /**
     * @Description: TODO(删除角色)
     * @return
     * @throws Exception
     * @author CAIYJ
     * @date 2018年4月11日 下午4:53:52
     * @lastModifier CAIYJ
     */
    @ApiOperation(value = "删除", notes = "删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = false, dataType = "Long", paramType = "path") })
    @RequestMapping(value = "/remove/{id}")
    public ReturnDTO<String> remove(@PathVariable Long id) throws Exception {
        if (imCenterRoleService.removeRole(id)) {
            return ReturnDTO.success("删除成功");
        }
        return ReturnDTO.fail(SystemCodeEnum.error_create_failed);
    }
}
